﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for Applicant
/// </summary>
public class Applicant
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
	public Applicant()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    private DataTable SelectUser()
    {
        DataTable tb = new DataTable();
        
        try
        {
            SqlCommand comm = new SqlCommand("SELECT * from SelectUserApplicant", conn);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            DataSet ds = new DataSet();
            da.Fill(ds);
            tb = ds.Tables[0];
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }
        return tb;
    }
    private string CheckUserAdd()
    {
        string sUser = "";
        try
        {
            DataTable tb = new DataTable();
            tb = SelectUser();
            if (tb.Rows.Count > 0)
            {
                string ID = tb.Rows[tb.Rows.Count - 1]["ApplicantID"].ToString();
                char[] cID = ID.ToArray();
                for (int i = (ID.Count() - 1); i >= 0; i--)
                {
                    try
                    {
                        int iu = Convert.ToInt32(cID[i].ToString());
                        if (iu < 9)
                        {
                            iu++;
                            sUser += iu;
                            for (int j = i - 1; j >= 0; j--)
                            {
                                sUser += cID[j].ToString();
                            }
                            break;
                        }
                        else
                        {
                            iu = 0;
                            sUser += iu;
                        }
                    }
                    catch (Exception)
                    {
                        sUser += cID[i].ToString();
                    }
                }
                char[] cUser = sUser.ToArray();
                sUser = "";
                for (int i = (cUser.Count() - 1); i >= 0; i--)
                {
                    sUser += cUser[i].ToString();
                }
            }
            else
            {
                sUser = "A0001";
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }
        return sUser;
    }
    public bool InsertApplicant(string _VacancyID, string _FullName, string _Address, string _Phone,  DateTime _BirthOfDate, string _EmailID)
    { 
        bool result = false;
        try
        {
            SqlCommand comm = new SqlCommand("EXECUTE CreateNewApplicant @ApplicantID, @VacancyID, @FullName, @Address, @Phone, @BirthOfDate, @EmailID, 3", conn);
            comm.Parameters.AddWithValue("@ApplicantID", CheckUserAdd());
            comm.Parameters.AddWithValue("@VacancyID", _VacancyID);
            comm.Parameters.AddWithValue("@FullName", _FullName);
            comm.Parameters.AddWithValue("@Address", _Address);
            comm.Parameters.AddWithValue("@Phone", _Phone);
            comm.Parameters.AddWithValue("@BirthOfDate", _BirthOfDate);
            comm.Parameters.AddWithValue("@EmailID", _EmailID);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            int iresult = comm.ExecuteNonQuery();
            if (iresult > 0)
            {
                result = true;
            }
        }
        catch (Exception    ex)
        {
            Console.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        return result;
    }
    


}